Learning PHP, MySQL & JavaScript by Robin Nixon
Author:Robin Nixon [Nixon, Robin]
Language: eng
Format: epub, mobi
Tags: www.it-ebooks.info
Published: 2015-01-14T00:23:21+00:00
If those two checks are satisfied, the test function is called to see whether any disal‐
lowed characters appear in the field. If any of these tests fail, an error message is
returned. The allowed characters in an email address are uppercase and lowercase let‐
ters, numbers, and the _, -, period, and @ characters, as detailed in the regular expres‐
sion passed to the test method. If no errors are found, the empty string is returned
to indicate successful validation. On the last line, the script and document are closed.
Figure 16-2 shows the result of the user clicking the Signup button without having
completed any fields.
Figure 16-2. JavaScript form validation in action
Using a separate JavaScript file
Of course, because they are generic in construction and could apply to many types of
validations you might require, these six functions make ideal candidates for moving
out into a separate JavaScript file. You could name the file something like vali‐
date_functions.js and include it right after the initial script section in Example 16-1,
using the following statement:
<script src="validate_functions.js"></script>
Regular Expressions
Let’s look a little more closely at the pattern matching we have been doing. We’ve
achieved it using regular expressions, which are supported by both JavaScript and
PHP. They make it possible to construct the most powerful of pattern-matching algo‐
rithms within a single expression.
Regular Expressions | 377
Matching Through Metacharacters
Every regular expression must be enclosed in slashes. Within these slashes, certain
characters have special meanings; they are called metacharacters. For instance, an
asterisk (*) has a meaning similar to what you have seen if you use a shell or Win‐
dows command prompt (but not quite the same). An asterisk means, “The text you’re
trying to match may have any number of the preceding characters—or none at all.”
For instance, let’s say you’re looking for the name Le Guin and know that someone
might spell it with or without a space. Because the text is laid out strangely (for
instance, someone may have inserted extra spaces to right-justify lines), you could
have to search for a line such as this:
The difficulty of classifying Le Guin's works
So you need to match LeGuin, as well as Le and Guin separated by any number of
spaces. The solution is to follow a space with an asterisk:
/Le *Guin/
There’s a lot more than the name Le Guin in the line, but that’s OK. As long as the
regular expression matches some part of the line, the test function returns a true
value. What if it’s important to make sure the line contains nothing but Le Guin? I’ll
show you how to ensure that later.
Suppose that you know there is always at least one space. In that case, you could use
the plus sign (+), because it requires at least one of the preceding characters to be
present:
/Le +Guin/
Fuzzy Character Matching
The dot (.) is particularly useful, because it can match anything except a newline.
Suppose that you are looking for HTML tags, which start with < and end with >. A
simple way to do so is shown here:
/<.*>/
The dot matches any character, and the * expands it to match zero or more charac‐
ters, so this is saying, “Match anything that lies between < and >, even if there’s noth‐
ing.
Download
Learning PHP, MySQL & JavaScript by Robin Nixon.mobi
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Access | Data Mining |
Data Modeling & Design | Data Processing |
Data Warehousing | MySQL |
Oracle | Other Databases |
Relational Databases | SQL |
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8258)
Azure Data and AI Architect Handbook by Olivier Mertens & Breght Van Baelen(6411)
Building Statistical Models in Python by Huy Hoang Nguyen & Paul N Adams & Stuart J Miller(6370)
Serverless Machine Learning with Amazon Redshift ML by Debu Panda & Phil Bates & Bhanu Pittampally & Sumeet Joshi(6259)
Data Wrangling on AWS by Navnit Shukla | Sankar M | Sam Palani(6035)
Driving Data Quality with Data Contracts by Andrew Jones(5999)
Learning SQL by Alan Beaulieu(5959)
Machine Learning Model Serving Patterns and Best Practices by Md Johirul Islam(5766)
Weapons of Math Destruction by Cathy O'Neil(5722)
Big Data Analysis with Python by Ivan Marin(5175)
Data Engineering with dbt by Roberto Zagni(4200)
Solidity Programming Essentials by Ritesh Modi(3839)
Time Series Analysis with Python Cookbook by Tarek A. Atwan(3690)
Pandas Cookbook by Theodore Petrou(3407)
Blockchain Basics by Daniel Drescher(3274)
Hands-On Machine Learning for Algorithmic Trading by Stefan Jansen(2885)
Feature Store for Machine Learning by Jayanth Kumar M J(2797)
Learn T-SQL Querying by Pam Lahoud & Pedro Lopes(2779)
Mastering Python for Finance by Unknown(2731)
